package com.hc.serviceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.hc.bean.GradeBean;
import com.hc.comm.Service.ComparatorGradeBeanl;
import com.hc.dao.RunSqlDao;
import com.hc.daoImpl.RunSqlDaoImpl;
import com.hc.service.CompanyGradeService;
import com.hc.service.GetDataService;
import com.hc.service.VlanGradeService;
import com.hc.util.DateUtil;
import com.hc.util.DateUtilExt;

public class GetDataServiceImpl implements GetDataService{

	
	public final static int pageCount = 18;
	
	@Override
	public List getRawData(String selectRawTestType,String companySelect,String startDate ,String stopDate,int pageNumber,String companyText) {
	
	
		
		int pageCount = 0 ;
		List list = null;
		if(pageNumber==1){
			
			
			pageCount=	getPageCount( selectRawTestType, companySelect, startDate , stopDate,companyText);
			list=	getData(selectRawTestType, companySelect, startDate , stopDate,pageNumber,companyText);
			
			if(list!=null){
				list.add(pageCount);
				
			}
			
		}else{
			
			list=	getData(selectRawTestType, companySelect, startDate , stopDate,pageNumber,companyText);
			
		}
		
		
		
		return list;

		
	}



	private List getData(String selectRawTestType,String companySelect,String startDate ,String stopDate,int pageNumber,String companyText) {

		String sql = null;
		
		
		if("网页测试".equals(selectRawTestType)){
			
			
			

				
				
				sql = "select w.* from ( select top "+pageCount+" a.indexe from ( select top "+(pageNumber*pageCount)+" indexe ,cmdTime from webTestData where cmdTime BETWEEN '"+startDate+"' and '"+stopDate+"'  " ;
						
				
			
		
			if("测试网址".equals(companySelect)){
				sql +=" and url like '%"+companyText+"%'";	
				
			}else if("宽带账号".equals(companySelect)){
				sql +=" and userNumber like '%"+companyText+"%'";	
				
			}else if("宽带套餐".equals(companySelect)){
				
				sql +=" and bSize like '%"+companyText+"%'";	
			}else if("VlanID1".equals(companySelect)){
				
				sql +=" and vlanId1 like '%"+companyText+"%'";	
			}else if("VlanID2".equals(companySelect)){
				sql +=" and vlanId2 like '%"+companyText+"%'";	
				
			}
			
			sql +="  ORDER BY cmdTime Desc) as a ORDER BY a.cmdTime asc) as b ,webTestData as w where" +
				" b.indexe = w.indexe  ORDER BY w.indexe DESC";
			
			
			
	
		}else if("下载测试".equals(selectRawTestType)){
				
					
					sql = "select w.* from ( select top "+pageCount+" a.indexe from ( select top "+(pageNumber*pageCount)+" indexe ,cmdTime from downFileTestData where  cmdTime BETWEEN '"+startDate+"' and '"+stopDate+"' ";
					
					 if("宽带账号".equals(companySelect)){
						sql +=" and userNumber like '%"+companyText+"%'";	
						
					}else if("宽带套餐".equals(companySelect)){
						
						sql +=" and bSize like '%"+companyText+"%'";	
					}else if("VlanID1".equals(companySelect)){
						
						sql +=" and vlanId1 like '%"+companyText+"%'";	
					}else if("VlanID2".equals(companySelect)){
						sql +=" and vlanId2 like '%"+companyText+"%'";	
						
					}
					
					
					sql +=	"  ORDER BY cmdTime Desc) as a ORDER BY a.cmdTime asc) as b ,downFileTestData as w where" +
					" b.indexe = w.indexe  ORDER BY w.indexe DESC";
				
			
		}
		
		
		if(sql!=null){
			RunSqlDao dao =new RunSqlDaoImpl();
			System.out.println(sql);
			List list = dao.runSqlQuery(sql);
		
				return list;
	
		}
		
		
		

		
		
		return null;
	}

	private int getPageCount(String selectRawTestType,String companySelect,String startDate ,String stopDate,String companyText) {

		String sql = null;
		if("网页测试".equals(selectRawTestType)){
			
		
			sql = "select count(indexe) from webTestData where cmdTime BETWEEN '"+startDate+"' and '"+stopDate+"'";
			
			if("测试网址".equals(companySelect)){
				sql +=" and url like '%"+companyText+"%'";	
				
			}else if("宽带账号".equals(companySelect)){
				sql +=" and userNumber like '%"+companyText+"%'";	
				
			}else if("宽带套餐".equals(companySelect)){
				
				sql +=" and bSize like '%"+companyText+"%'";	
			}else if("VlanID1".equals(companySelect)){
				
				sql +=" and vlanId1 like '%"+companyText+"%'";	
			}else if("VlanID2".equals(companySelect)){
				sql +=" and vlanId2 like '%"+companyText+"%'";	
				
			}
			
		}else if("下载测试".equals(selectRawTestType)){
			
			sql = "select count(indexe) from downFileTestData where cmdTime BETWEEN '"+startDate+"' and '"+stopDate+"' ";
			 if("宽带账号".equals(companySelect)){
					sql +=" and userNumber like '%"+companyText+"%'";	
					
			}else if("宽带套餐".equals(companySelect)){
					
					sql +=" and bSize like '%"+companyText+"%'";	
			}else if("VlanID1".equals(companySelect)){
					
					sql +=" and vlanId1 like '%"+companyText+"%'";	
			}else if("VlanID2".equals(companySelect)){
					sql +=" and vlanId2 like '%"+companyText+"%'";	
					
			}
		}
		
		
		if(sql!=null){
			RunSqlDao dao =new RunSqlDaoImpl();
			System.out.println(sql);
			List pages = dao.runSqlQueryNoKey(sql);
			List page =  (List) pages.get(0);
			int pageNumber =Integer.parseInt( page.get(0).toString());
			
			if(pageNumber%pageCount==0){
				
				return pageNumber/pageCount;
				
			}else{
				
				return (pageNumber/pageCount)+1;
			}
			
			
		}
		
		
		
		return 0;
	}

	@Override
	public List getGradeData(String startDate,String stopDate) {



		
		CompanyGradeService service = new CompanyGradeServiceImpl(); 
		
		
//		GradeBean allCompanyGrade = service.getAllCompanyGrade(startDate, stopDate);
		List<GradeBean> list = service.getCompanyGrade(startDate, stopDate);
		
		
		ComparatorGradeBeanl comparator = new ComparatorGradeBeanl();
		
		if(list!=null){
			
			Collections.sort(list,comparator);
			GradeBean allCompanyGrade  = getGradeCount(list);
			list.add(0,allCompanyGrade );
		}
	
		
		
		return list;
		

	}
	
	public GradeBean getGradeCount(List<GradeBean> list){

		double downFileGrade  = 0;
		int downFileTestCount = 0;
		double webtimedelayGrade = 0;
		int webTestCount = 0;
		double weblostRateGrade = 0;
		double webShakeGrade = 0 ;
		String date = null;
		for(GradeBean gradeBean:list){
			downFileGrade+=gradeBean.getDownFileGrade();
			downFileTestCount+=gradeBean.getDownFileTestCount();
			webtimedelayGrade+=gradeBean.getWebtimedelayGrade();
			weblostRateGrade+=gradeBean.getWeblostRateGrade();
			webShakeGrade+=gradeBean.getWebShakeGrade();
			webTestCount+=gradeBean.getWebTestCount();
			if(date ==null){
				date = gradeBean.getTime();
			}
		}
		
		GradeBean allCompanyGradeBean = new GradeBean();
		
		allCompanyGradeBean.setId(0);
		
		int listSize = list.size();
	
			allCompanyGradeBean.setDownFileGrade((int) (downFileGrade/listSize));

		
		
		allCompanyGradeBean.setDownFileTestCount(downFileTestCount);
		
		
			allCompanyGradeBean.setWebtimedelayGrade((int) (webtimedelayGrade/listSize));
			allCompanyGradeBean.setWebShakeGrade((int) (webShakeGrade/listSize));
			allCompanyGradeBean.setWeblostRateGrade((int) (weblostRateGrade/listSize));
		
		
		allCompanyGradeBean.setWebTestCount(webTestCount);
		allCompanyGradeBean.setVlanId("-");
		allCompanyGradeBean.setTime(date);
		
		
		return allCompanyGradeBean;

	}
	
	

	@Override
	public List getVlanGradeData(String startDate ,String stopDate  ,String vlanGradeCompanyTextSelect,String vlanGradeCompanyText) {

		VlanGradeService service = new VlanGradeServiceImpl(); 
		List<GradeBean> list = service.getVlanGrade(startDate, stopDate,vlanGradeCompanyTextSelect,vlanGradeCompanyText);
		ComparatorGradeBeanl comparator = new ComparatorGradeBeanl();
		Collections.sort(list,comparator);

	  return list;
	}



}
